package com.zhuang.controller;

import com.zhuang.dto.CommentDTO;
import com.zhuang.dto.ResultDTO;
import com.zhuang.exception.CustomizeErrorCode;
import com.zhuang.exception.CustomizeException;
import com.zhuang.pojo.Comment;
import com.zhuang.pojo.User;
import com.zhuang.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    CommentService commentService;

    @PostMapping("/comment")
    @ResponseBody
    public Object comment(@RequestBody Comment comment,
                          HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            throw new CustomizeException(CustomizeErrorCode.NOT_LOGIN);
        }
        comment.setGmtCreate(System.currentTimeMillis());
        comment.setGmtModified(comment.getGmtCreate());
        comment.setCommentator(user.getAccountId());
        commentService.insert(comment);
        return ResultDTO.okOf();
    }

    @GetMapping("/comment/{id}")
    @ResponseBody
    public ResultDTO<List> commentList(@PathVariable(name = "id" ) Integer id) {
        List<CommentDTO> commentList = commentService.getByParentId(id, 2);

        return ResultDTO.okOf(commentList);
    }
}
